import java.util.concurrent.*;

/**
 * @Auther: zch
 * @Date: 2019/1/8 10:02
 * @Description:  自定义线程池 异常堆栈打印
 */
public class TraceThreadPollExecutor extends ThreadPoolExecutor {
    public TraceThreadPollExecutor(int corePoolSize, int maximumPoolSize, long keepAliveTime, TimeUnit unit, BlockingQueue<Runnable> workQueue) {
        super(corePoolSize, maximumPoolSize, keepAliveTime, unit, workQueue);
    }

    @Override
    public void execute(Runnable command) {
        super.execute(warp(command,clientTrace(),Thread.currentThread().getName()));
    }

    @Override
    public Future<?> submit(Runnable task) {
        return super.submit(warp((task), clientTrace(), Thread.currentThread().getName()));
    }

    private Exception clientTrace() {
        return new Exception("Client stack trace");
    }

    private Runnable warp(final Runnable task, final Exception cilentStask, String clientThreadName) {
        return new Runnable() {
            @Override
            public void run() {
                try {
                    task.run();
                } catch (Exception e) {
                    cilentStask.printStackTrace();
                    throw e;
                }
            }
        };
    }
      public static void main(String[] args) throws InterruptedException
          {
              ThreadPoolExecutor pools=new TraceThreadPollExecutor(0,Integer.MAX_VALUE,0L,TimeUnit.SECONDS,new SynchronousQueue<Runnable>());
              for (int i=0;i<5;i++) {
                  pools.execute(()-> System.out.println("helo"+Thread.currentThread().getId()));
              }

          }
}
